package net.chrisrichardson.ec2deployer.applications

import groovy.mock.interceptor.MockFor
import net.chrisrichardson.ec2deployer.cluster.WebServerTier
import net.chrisrichardson.ec2deployer.core.EC2Server

class JmeterApplicationTest extends GroovyTestCase {

    def mockEc2Server
    def mockWebServerTier

    public void testRun() {
        mockEc2Server = new MockFor(EC2Server)
        mockWebServerTier = new MockFor(WebServerTier)

        mockEc2Server.demand.scp { from, to ->
         assertEquals "foo.jmx", from
         assertEquals "/tmp", to
        }
        mockWebServerTier.demand.getPrivateDnsName { "foo.com" }
        mockWebServerTier.demand.getPort { 80 }
        mockEc2Server.demand.ssh { cmd ->
         assertEquals 'export PATH=$PATH:$JAVA_HOME/bin ; rm -f /tmp/foo.jtl ; /var/lib/jakarta-jmeter-2.2/bin/jmeter -n -t /tmp/foo.jmx -l /tmp/foo.jtl -Jiterations=100 -JthreadCount=9 -Jhost=foo.com -Jport=80', cmd
        }
        mockEc2Server.demand.getInstanceId { "i-foo" }
        mockEc2Server.demand.scpDown { from, to ->
         assertEquals "/tmp/foo.jtl", from
         assertTrue to.startsWith("target/")
        }


        mockEc2Server.use {
            mockWebServerTier.use {
                def ec2Server = new EC2Server()
                def webServerTier = new WebServerTier()
                def jmeterApplication = new JmeterApplication(ec2Server, webServerTier)

                def jmxFileName = "foo.jmx"
                def prefix = "blah-"
                def threadCount = [9]
                
                jmeterApplication.run(jmxFileName, prefix, threadCount)
            }
        }
    }

}